<%@page import="admin.data.AdsDealData"%>
<%@page import="models.AdsDealModel"%>
<%@page import="helpers.SessionHelper"%>
<%@page import="models.GeoModel"%>
<%@page import="data.DealData"%>
<%@page import="java.util.Calendar"%>
<%@page import="java.util.Locale"%>
<%@page import="java.text.NumberFormat"%>
<%@page import="com.google.appengine.api.blobstore.BlobKey"%>
<%@page import="com.google.appengine.api.images.ImagesServiceFactory"%>
<%@page import="com.google.appengine.api.images.ImagesService"%>
<%@page import="java.util.ArrayList"%>
<%@page import="models.DealModel"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%	
	DealData dealData = new DealData();		
	if(request.getAttribute("dealData")!=null){
		dealData = (DealData)request.getAttribute("dealData"); 
	}	
	String url = (String) request.getAttribute("url"); 
	List<DealModel> list = dealData.getListResult();
	ImagesService imagesService = ImagesServiceFactory.getImagesService();
	NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);  
	String recentCategory = (String)request.getAttribute("recentCategory");
	GeoModel currentGeo = SessionHelper.getCurrentCity(request);
	DealModel dealMarketing = dealData.getDealMarketing(currentGeo.getGeoId(),recentCategory);		
%>
<jsp:include page="/layouts/header.jsp">
	<jsp:param name="language" value="1" />
</jsp:include>
        	<div id="content_inner">
        		<% if (session.getAttribute("success") != null) { %>
	            	<div class="notification-s">
	            		<img src="/images/icon_close.gif" />
	            		<%=session.getAttribute("success") %>
	            	</div>
	            	<% session.removeAttribute("success"); %>
	            <% } %>
	            <% if (session.getAttribute("error") != null) { %>
	            	<div class="notification-e">
	            		<img src="/images/icon_close.gif" />
	            		<%=session.getAttribute("error") %>
	            		<% session.removeAttribute("error"); %>
	            	</div>
	            <% } %>
            	<div id="dealToday">
            		<%
            			AdsDealModel adSpecial = AdsDealData.getByType("special");
            			List<DealModel> homeAdvertising = AdsDealData.getListDealByType("special"); 
            			if(homeAdvertising!=null&&adSpecial!=null){
            				if(adSpecial.getImages().size()>0){	 
            						String image = "";
	                         		BlobKey blobKey = new BlobKey(adSpecial.getImages().get(0));   
	                    			try{
	                    				image = imagesService.getServingUrl(blobKey);
	                    			}catch(Exception e){
	                    				image = "";
	                    			}
	                    			if(image!=null&&image.length()>0){
	                         		%>
	                         		<div class="advTop">
	                         			<img  src="<%=image %>" alt="<%=homeAdvertising.get(0).getShortName()%>" title="<%=homeAdvertising.get(0).getShortName()%>"  class="img-full-width" />				                    	
				                    </div>	                         		
	                         		<%
	                    			}	                         	                   
                         	}
            		%>
	            		
                    <%
            			}
                    %>
                    <jsp:include page="/elements/list_category.jsp"/>
                    <%
                    	if(dealMarketing!=null&&dealMarketing.getDealId().length()>0){
                    		String img;
                    		String[] split = dealMarketing.getImageDeal().split(",");
                			if(split.length>0){
                				img = split[0];			
                			}else{
                				img = dealMarketing.getImageDeal();
                			}                	
                			BlobKey blobKey = new BlobKey(img);   
                			try{
                				img = imagesService.getServingUrl(blobKey);
                			}catch(Exception e){
                				img = "";
                			}
                    %>
                    <div id="featureProduct" class="clearafter">
                    	<div class="productImage left">
                        	<a href="/<%=dealMarketing.getDealId()%>/<%=dealMarketing.getAlias()%>.html"><img src="<%=img %>" alt="<%=dealMarketing.getShortName()%>" title="<%=dealMarketing.getShortName()%>" /></a>
                        </div>
                        <div class="right productInfo">
                        	<h2 class="productTitle"><a href="/<%=dealMarketing.getDealId()%>/<%=dealMarketing.getAlias()%>.html"><%=dealMarketing.getShortName() %></a></h2>
                            <div class="deliveryType voucher" style="color: black">
								<%
                               		switch(dealMarketing.getProductType()){
                               			case 0:%>(Giao voucher)<%
                               			break;
                               			case 1:%>(Phiếu điện tử)<%
                               			break;
                               			case 2:%>(Giao sản phẩm)<%
                               			break;
                               		}
                               	%>            
							</div>
                            <div class="productDescription">
                            	<%=dealMarketing.getName() %> 
                            </div>
                            <div class="productPrice">
                            	<span class="newPrice"><%=nf.format(dealMarketing.getPriceDiscount()) %> VNĐ</span>
                                <br />
                                Giá gốc: <span class="oldPrice"><%=nf.format(dealMarketing.getPrice()) %> VNĐ</span>
                            </div>
                            <div class="productExInfo clearafter">
                            	<%                                		 
                               		int discount = 0;
                               		try{
                               			discount = (dealMarketing.getPrice() - dealMarketing.getPriceDiscount())*100/dealMarketing.getPrice();
                               		}catch(Exception ex){
                               			discount = 0;
                               		}                                		
                               	%>
                                <div class="product_save_percent">
                                    Tiết kiệm
                                    <span><%=discount%>%</span>
                                </div>
                                <div class="product_bought">
                                    Số người đã mua
                                    <span><%=dealMarketing.getUserAmount() %></span>
                                </div>
                                <%
                                	if(dealMarketing.isDisplayTime()){
                                %>
                                <div class="product_timeout">
                                    Thời gian còn lại
                                    <span class="countdown" id="counter<%=dealMarketing.getDealId()%>"></span>
                                       <%
                                       	Calendar cal = Calendar.getInstance();
                                       	cal.setTime(dealMarketing.getEndTime());
                                       %>                                                                                                                                                                                   
                                      <script type="text/javascript">
                                      var deathTime = new Date(<%=cal.get(Calendar.YEAR) %>, <%=cal.get(Calendar.MONTH)%>, <%=cal.get(Calendar.DATE) %>, <%=cal.get(Calendar.HOUR_OF_DAY)%>, <%=cal.get(Calendar.MINUTE)%>, <%=cal.get(Calendar.SECOND)%>);
                                        <%
                                        	if(cal.get(Calendar.DATE)==Calendar.getInstance().get(Calendar.DATE)){
                                        %>
                                        $("#counter<%=dealMarketing.getDealId()%>").countdown({until: deathTime, compact: true, format: "HMS", description: "", layout: "{hnn}:{mnn}:{snn}"});
                                        <%
                                        	}else{
                                        %>
                                        $("#counter<%=dealMarketing.getDealId()%>").countdown({until: deathTime, compact: true, format: "d", description: "", layout: "{dnn} ngày"});
                                        <%		
                                        	}
                                        %>
                                        
								    </script>      
                                </div>
                                <%
                                	}
                                %>
                            </div>
                            <a href="/<%=dealMarketing.getDealId()%>/<%=dealMarketing.getAlias()%>.html" class="viewProduct button green">xem</a>
                        </div>
                    </div>
                    <%
                    	}
                    %>
                    <div id="productListing" class="clearafter">
                    	<%
                    		if(list!=null&&list.size()>0){
                    			for(DealModel deal: list){
                    	%>
                    	<div class="productItems">
                        	<div class="productItem_inner">
                            	<h3 class="productTitle">
                            		<a href="/<%=deal.getDealId()%>/<%=deal.getAlias()%>.html">
                            			<%=deal.getSubShortName() %>
                            		</a>
                           		</h3>                           
                                <div class="productImage">
                                	<%
                                		String img = "";
                                		if(deal.getImageDeal()!=null&&deal.getImageDeal().length()>0){
                                			String[] split = deal.getImageDeal().split(",");
                                			if(split.length>0){
                                				img = split[0];			
                                			}else{
                                				img = deal.getImageDeal();
                                			}
                                	
                                			BlobKey blobKey = new BlobKey(img);
                                			try{
                                				img = imagesService.getServingUrl(blobKey);
                                			}catch(Exception e){
                                				img = "";
                                			}
                                			%>
                                				<a href="/<%=deal.getDealId()%>/<%=deal.getAlias()%>.html">
                               						<img src="<%=img %>" style="width: 280px; height: 206px"/>
                               					</a>
                                			<%
                                		}else{
                                			%>
                                				<a href="/<%=deal.getDealId()%>/<%=deal.getAlias()%>.html"><img src="" style="width: 280px; height: 206px"/></a>
                                			<%
                                		}                                			
                                	%>
                                	<div class="deliveryTypeWrap">
                                        <div class="deliveryType voucher">
		                                	<%
		                                		switch(deal.getProductType()){
		                                			case 0:%>(Giao voucher)<%
		                                			break;
		                                			case 1:%>(Giao sản phẩm)<%
		                                			break;		                                			
		                                		}
		                                	%>                                    	                         
		                                </div>
                                    </div>
                                </div>
                                <div class="productDescription">
                                	<%=deal.getSubName()%> 
                                </div>
                                <div class="productPrice clearafter">
                                	<%                                		 
                                		int discount = 0;
                                		try{
                                			discount = (deal.getPrice() - deal.getPriceDiscount())*100/deal.getPrice();
                                		}catch(Exception ex){
                                			discount = 0;
                                		}                                		
                                	%>
                                	<div class="left">
                                        <span class="newPrice"><%=nf.format(deal.getPriceDiscount())%> VNĐ</span>
                                        <br />
                                        Giá gốc: <span class="oldPrice"><%=nf.format(deal.getPrice())%> VNĐ</span>
                                    </div>                                    
                                    <a href="/<%=deal.getDealId()%>/<%=deal.getAlias()%>.html" class="viewProduct button green right">Xem</a>
                                </div>
                                <div class="productExInfo clearafter">
                                	<div class="product_save_percent">
                                    	Tiết kiệm
                                        <span><%=discount%>%</span>
                                    </div>
                                    <div class="product_bought">
										Số người đã mua
										<span><%=deal.getUserAmount() %></span>
                                    </div>        
                                    <%
                                    	if(deal.isDisplayTime()){
                                   	%>                            
                                    <div class="product_timeout">
                                    	Thời gian còn lại
                                        <span class="countdown" id="counter<%=deal.getDealId()%>"></span>
                                        <%
                                        	Calendar cal = Calendar.getInstance();
                                        	cal.setTime(deal.getEndTime());
                                        %>                                                                                                                                                                                   
                                        <script type="text/javascript">
                                        var deathTime = new Date(<%=cal.get(Calendar.YEAR) %>, <%=cal.get(Calendar.MONTH)%>, <%=cal.get(Calendar.DATE) %>, <%=cal.get(Calendar.HOUR_OF_DAY)%>, <%=cal.get(Calendar.MINUTE)%>, <%=cal.get(Calendar.SECOND)%>); 	                                        	                                       	                                        
	                                        <%
	                                        	if(cal.get(Calendar.DATE)==Calendar.getInstance().get(Calendar.DATE)){
	                                        %>
	                                        $("#counter<%=deal.getDealId()%>").countdown({until: deathTime, compact: true, format: "HMS", description: "", layout: "{hnn}:{mnn}:{snn}"});
	                                        <%
	                                        	}else{
	                                        %>
	                                        $("#counter<%=deal.getDealId()%>").countdown({until: deathTime, compact: true, format: "d", description: "", layout: "{dnn} ngày"});
	                                        <%		
	                                        	}
	                                        %>
									    </script>                                         
                                    </div>
                                    <%
                                    	}
                                    %>
                                </div>
                            </div>
                        </div>
                       <%
                    			}
                    		}
                       %>                                          
                </div>
                <ul class="paginate">                   
                  	<%
                  		if(dealData.getTotalPage()>1){
            		%>
                  				<li class="page">Trang: </li>   
                  	<%
                  			for(int i = 1;i<=dealData.getTotalPage();i++){
                  				if(dealData.getPage()==i){
                  	%>  
                  				<li class="active"><span><%=i %></span></li>
                  	<%
                  				}else{
                  	%>
               					<li><a href="<%=url%>?page=<%=i%>"><%=i %></a></li>
                  	<%
                  				}                  	
                  			}
                  		}
                    %>
                 </ul>
            </div>
        
<jsp:include page="/layouts/footer.jsp">
    <jsp:param name="language" value="1" />
</jsp:include>